* Title : De Micheli (2018) Replication Code
* Date : August 2017
* Article title : The Racialized Effects of Social Programs in Brazil
* Journal : Latin American Politics and Society
* Author : David De Micheli

******************************************************************************
*2008
******************************************************************************
use "2008.dta", clear
set more off
rename _all, lower

gen region = .
replace region = 1 if estratopri==1501
replace region = 2 if estratopri==1502
replace region = 3 if estratopri==1503
replace region = 4 if estratopri==1504
replace region = 5 if estratopri==1505

gen female = .
replace female=1 if q1==2
replace female=0 if q1==1

gen partyid = .
replace partyid = 4 if vb11 > 1510 & vb11 < 1566
replace partyid = 4 if vb11==77
replace partyid = 0 if vb10==2 | vb11==0
replace partyid = 1 if vb11==1513
replace partyid = 2 if vb11==1545
replace partyid = 3 if vb11==1515

gen race2 = .
replace race2 = 0 if etid==1
replace race2 = 1 if etid==2 | etid==4

gen educ = ed

gen age = .
replace age = q2
gen age2 = age^2

gen residents = .
replace residents = 1 if q11==1 & q12==0 /*Single no kids*/
replace residents = 1 if q11==4 & q12==0 /*Divorced no kids*/
replace residents = 1 if q11==5 & q12==0 /*Separated no kids*/
replace residents = 1 if q11==6 & q12==0 /*Widow(er) no kids*/
replace residents = 2 + q12a if q11==2 | q11==3 

gen income1 = .
replace income1 = 0 if q10==1 
replace income1 = 190 if q10==2 
replace income1 = 570.01 if q10==3 
replace income1 = 950.01 if q10==4 
replace income1 = 1520.01 if q10==5 
replace income1 = 2280.01 if q10==6 
replace income1 = 2850.01 if q10==7 
replace income1 = 3800.01 if q10==8 
replace income1 = 5130.01 if q10==9 
replace income1 = 6650.01 if q10==10
replace income1 = 7600.01 if q10==11

gen income4 = income1/residents
gen income5 = .
replace income5 = ln(income4) if income4!=0
replace income5 = ln(1) if income4==0
replace income5 = . if income4==.
drop income4
rename income5 income4

factor  r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15, pcf factors(1)
predict wealth1
xtile wealth1q = wealth1, nquantiles(5)
drop wealth1
rename wealth1q wealth1

gen vote6 = .
replace vote6 = 0 if vb20==1 | vb20==4
replace vote6 = 1 if vb20==2 | vb20==3

gen vote7 = .
replace vote7 = 0 if vb20==3
replace vote7 = 1 if vb20==2

gen bf1 = .
replace bf1 = 1 if brabf1a==1
replace bf1 = 0 if brabf1a==2

keep year region female partyid race2 educ age age2 income4 wealth1 vote6 vote7 bf1

save "2008_cleaned.dta", replace

******************************************************************************
*2010
******************************************************************************

use "2010.dta", clear
set more off
rename _all, lower

gen year = 2010

gen region = .
replace region = 1 if estratopri==1501
replace region = 2 if estratopri==1502
replace region = 3 if estratopri==1503
replace region = 4 if estratopri==1504
replace region = 5 if estratopri==1505

gen female = .
replace female=1 if q1==2
replace female=0 if q1==1

gen partyid = .
replace partyid = 4 if vb11 > 1503 & vb11 < 1515
replace partyid = 0 if vb10==2
replace partyid = 1 if vb11==1501
replace partyid = 2 if vb11==1503
replace partyid = 3 if vb11==1502

gen race2 = .
replace race2 = 0 if etid==1
replace race2 = 1 if etid==2 | etid==4

gen educ = .
replace educ = ed

gen age = .
replace age = q2
gen age2 = age^2

gen residents = .
replace residents = q13bra

gen income1 = .
replace income1 = 0 if q10==0
replace income1 = 255 if q10==1
replace income1 = 765.01 if q10==2
replace income1 = 1275.01 if q10==3
replace income1 = 2040.01 if q10==4 
replace income1 = 3060.01 if q10==5 
replace income1 = 3825.01 if q10==6 
replace income1 = 5100.01 if q10==7 
replace income1 = 6885.01 if q10==8 
replace income1 = 8925.01 if q10==9
replace income1 = 10200.01 if q10==10

gen income4 = income1/residents
gen income5 = .
replace income5 = ln(income4) if income4!=0
replace income5 = ln(1) if income4==0
replace income5 = . if income4==.
drop income4
rename income5 income4

factor  r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15, pcf factors(1)
predict wealth1
xtile wealth1q = wealth1, nquantiles(5)
drop wealth1
rename wealth1q wealth1

gen vote6 = .
replace vote6 = 0 if vb20==1 | vb20==4
replace vote6 = 1 if vb20==2 | vb20==3

gen vote7 = .
replace vote7 = 0 if vb20==3
replace vote7 = 1 if vb20==2

gen bf1 = .
replace bf1 = 1 if cct1bra==1
replace bf1 = 0 if cct1bra==2

keep year region female partyid race2 educ age age2 income4 wealth1 vote6 vote7 bf1

save "2010_cleaned.dta", replace

******************************************************************************
*2012
******************************************************************************
use "2012.dta", clear
set more off
rename _all, lower
label drop _all

gen date = .

gen region = .
replace region = 1 if estratopri==1501
replace region = 2 if estratopri==1502
replace region = 3 if estratopri==1503
replace region = 4 if estratopri==1504
replace region = 5 if estratopri==1505

gen female = .
replace female=1 if q1==2
replace female=0 if q1==1

gen partyid = .
replace partyid = 4 if vb11 > 1503 & vb11 < 1515
replace partyid = 0 if vb10==2
replace partyid = 1 if vb11==1501
replace partyid = 2 if vb11==1503
replace partyid = 3 if vb11==1502

gen race2 = .
replace race2 = 0 if etid==1
replace race2 = 1 if etid==4 | etid==5

gen educ = .
replace educ = ed 
replace educ = . if ed==.a | ed==.b

gen age = .
replace age = q2
gen age2 = age^2

gen residents = .
replace residents = q12c
replace residents = . if q12c==.b

rename q10new q10
gen income1 = .
replace income1 = 0 if q10==0
replace income1 = 50 if q10==1
replace income1 = 155 if q10==2
replace income1 = 260.5 if q10==3
replace income1 = 360.5 if q10==4 
replace income1 = 465.5 if q10==5 
replace income1 = 570.5 if q10==6 
replace income1 = 675.5 if q10==7 
replace income1 = 775.5 if q10==8 
replace income1 = 875.5 if q10==9
replace income1 = 985.5 if q10==10
replace income1 = 1085.5 if q10==11
replace income1 = 1185.5 if q10==12
replace income1 = 1320.5 if q10==13
replace income1 = 1475.5 if q10==14
replace income1 = 1705.5 if q10==15
replace income1 = 1860 if q10==16

gen income4 = income1/residents
gen income5 = .
replace income5 = ln(income4) if income4!=0
replace income5 = ln(1) if income4==0
replace income5 = . if income4==.
drop income4
rename income5 income4

factor  r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15, pcf factors(1)
predict wealth1
xtile wealth1q = wealth1, nquantiles(5)
drop wealth1
rename wealth1q wealth1

gen vote6 = .
replace vote6 = 0 if vb20==1 | vb20==4
replace vote6 = 1 if vb20==2 | vb20==3

gen vote7 = .
replace vote7 = 0 if vb20==3
replace vote7 = 1 if vb20==2

gen bf1 = .
replace bf1 = 1 if cct1b==1
replace bf1 = 0 if cct1b==2

keep year region female partyid race2 educ age age2 income4 wealth1 vote6 vote7 bf1

save "2012_cleaned.dta", replace

******************************************************************************
*2014
******************************************************************************
use "2014.dta", clear
set more off
rename _all, lower
label drop _all

gen year = 2014

gen region = .
replace region = 1 if estratopri==1501
replace region = 2 if estratopri==1502
replace region = 3 if estratopri==1503
replace region = 4 if estratopri==1504
replace region = 5 if estratopri==1505

gen female = .
replace female=1 if q1==2
replace female=0 if q1==1

gen partyid = .
replace partyid = 4 if vb11 > 1503 & vb11 < 1578
replace partyid = 0 if vb10==2
replace partyid = 1 if vb11==1501
replace partyid = 2 if vb11==1503
replace partyid = 3 if vb11==1502

gen race2 = .
replace race2 = 0 if etid==1
replace race2 = 1 if etid==4 | etid==5

gen educ = .
replace educ = ed 
replace educ = . if ed==.a | ed==.b

gen age = .
replace age = q2
gen age2 = age^2

gen residents = .
replace residents = q12c

rename q10new q10
gen income1 = .
replace income1 = 0 if q10==0
replace income1 = 250 if q10==1
replace income1 = 600.5 if q10==2
replace income1 = 750.5 if q10==3
replace income1 = 850.5 if q10==4 
replace income1 = 950.5 if q10==5 
replace income1 = 1050.5 if q10==6 
replace income1 = 1150.5 if q10==7 
replace income1 = 1300.5 if q10==8 
replace income1 = 1500.5 if q10==9
replace income1 = 1700.5 if q10==10
replace income1 = 1900.5 if q10==11
replace income1 = 2550.5 if q10==12
replace income1 = 3700.5 if q10==13
replace income1 = 4850.5 if q10==14
replace income1 = 6000.5 if q10==15
replace income1 = 6600 if q10==16

gen income4 = income1/residents
gen income5 = .
replace income5 = ln(income4) if income4!=0
replace income5 = ln(1) if income4==0
replace income5 = . if income4==.
drop income4
rename income5 income4

factor  r1 r3 r4 r4a r5 r6 r7 r8 r12 r14 r15, pcf factors(1)
predict wealth1
xtile wealth1q = wealth1, nquantiles(5)
drop wealth1
rename wealth1q wealth1

gen vote6 = .
replace vote6 = 0 if vb20==1 | vb20==4
replace vote6 = 1 if vb20==2 | vb20==3

gen vote7 = .
replace vote7 = 0 if vb20==3
replace vote7 = 1 if vb20==2

gen bf1 = .
replace bf1 = 1 if cct1b==1
replace bf1 = 0 if cct1b==2

keep year region female partyid race2 educ age age2 income4 wealth1 vote6 vote7 bf1

save "2014_cleaned.dta", replace

